<?php
/**
 * @author Zwc
 * Created On 2021/2/19 21:23
 */

namespace app\auth\service;


use app\common\enums\DelFlagEnum;
use app\common\models\MenuModel;
use think\facade\Log;

class MenuService extends BaseService
{
    protected $model;
    public function __construct()
    {
        $this->model = new MenuModel();
        parent::__construct();
    }

    public function getData($params) {
        $condition = $this->getCondition($params);
        return $this->model->where($condition)->order('type asc,sort desc')->select();
    }

    /**
     * 获取查询条件
     * @param $params
     * @return array
     */
    private function getCondition ($params) {
        $condition = [
            'del_flag' => 0
        ];
        return $condition;
    }

    public function getAllMenu() {
        $condition = [
        	['del_flag','=', DelFlagEnum::No],
			['is_menu','=', '1']
        ];
        $data = $this->model->where($condition)->order('type asc,sort desc')->select();
        // 格式化菜单
        $data = MenuFormat::formatMenu($data);
        return $data;
    }

    /**
     * 获取树形菜单
     */
    public function getMenuTree(): array
    {
        $condition = [
            'del_flag' => 0,
            'is_menu' => 1
        ];
        return $this->model->where($condition)->field('id,title,pid as pId,\'menu\' as type')->order('sort desc')->select()->toArray();
    }

    /**
     * 添加菜单
     * @param $params
     * @return array|mixed|void
     */
    public function add($params) {
        $data = $params;
        unset($data['actionList']);
        // 添加菜单
        try {
            $id = $this->model->insertGetId($data);
        }catch (\Exception $e) {
            Log::error('操作失败,错误信息:'.$e->getMessage());
            return ['code' => 1,'msg' => '操作失败,错误信息:'.$e->getMessage()];
        }
        return ['code' => 0,'msg' => '操作成功'];
    }

    /**
     * 菜单详情
     * @param $id
     * @return array
     */
    public function detail($id) : array
    {
        $where = [
            'id' => $id
        ];
        return $this->model->where($where)->find()->toArray();
    }

    /**
     * 编辑菜单
     * @param $params
     */
    public function edit($params) : array
    {
        $data = $params;
        unset($data['actionList']);
        try {
            $this->model->where(['id' => $params['id']])->save($data);
        }catch (\Exception $e) {
            Log::error('操作失败,错误信息:'.$e->getMessage());
            return ['code' => 1,'msg' => '操作失败,错误信息:'.$e->getMessage()];
        }
        return ['code' => 0,'msg' => '操作成功'];
    }

    /**
     * 根据URL获取连接的动作
     * @param $url
     */
    public function getMenuInfoByUri($url) {
        // 如果最后5位是.html,就要过滤掉
        if(mb_substr($url,strlen($url)-5,5) == '.html') {
            $url = mb_substr($url,0,-5);
        }
        $condition = [
            ['del_flag','=', 0],
            ['url','=',$url]
        ];
        return $this->model->where($condition)->find();
    }

	/**
	 * 根据URL,获取菜单
	 * @param $url
	 * @return array|\think\Model|null
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
    public function getMenuData($url) {
		$condition = [
			['del_flag', '=', DelFlagEnum::Yes],
			['url', '=', $url]
		];
		return $this->model->where($condition)->find();
	}
}